Page({
  data: {
    currentTime: '12:30',
    currentFilter: 'all',
    searchKeyword: '',
    
    // 所有订单数据
    allOrders: [],
    
    // 今日订单
    todayOrders: [],
    
    // 历史订单
    historyOrders: [],
    
    // 筛选后的订单列表
    filteredOrders: []
  },
  
  onLoad: function() {
    // 更新当前时间
    this.updateCurrentTime();
    
    // 获取订单数据
    this.fetchOrdersData();
  },
  
  // 更新当前时间
  updateCurrentTime: function() {
    const date = new Date();
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    this.setData({
      currentTime: `${hours}:${minutes}`
    });
  },
  
  // 获取订单数据
  fetchOrdersData: function() {
    // 这里应该调用实际的API获取订单数据
    // 暂时使用模拟数据
    const mockOrders = [
      {
        id: '001',
        orderNumber: 'OD2403220001',
        status: 'unpaid',
        statusText: '待付款',
        client: {
          id: '001',
          name: '周小姐'
        },
        items: [
          {
            id: '101',
            name: '法式蕾丝鱼尾婚纱',
            color: '象牙白',
            size: '定制',
            price: '18,800',
            image: 'https://images.unsplash.com/photo-1594612658290-386d64b8dd1c?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80'
          }
        ],
        totalPrice: '18,800',
        createTime: '2024-03-22',
        isToday: true,
        primaryAction: 'payment',
        primaryActionText: '收款'
      },
      {
        id: '002',
        orderNumber: 'OD2403220002',
        status: 'paid',
        statusText: '已付款',
        client: {
          id: '002',
          name: '李先生 & 王小姐'
        },
        items: [
          {
            id: '102',
            name: '白色缎面修身礼服套装',
            color: '纯白',
            size: '180/100A',
            price: '12,600',
            image: 'https://images.unsplash.com/photo-1596444382656-f81a489d9190?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80'
          },
          {
            id: '103',
            name: '星光奢华晚礼服',
            color: '香槟金',
            size: '165/M',
            price: '15,800',
            image: 'https://images.unsplash.com/photo-1599387737494-d81825f5cd70?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80'
          }
        ],
        totalPrice: '28,400',
        createTime: '2024-03-22',
        isToday: true,
        primaryAction: 'appointment',
        primaryActionText: '安排试装'
      },
      {
        id: '003',
        orderNumber: 'OD2403180008',
        status: 'completed',
        statusText: '已完成',
        client: {
          id: '003',
          name: '赵先生 & 钱小姐'
        },
        items: [
          {
            id: '104',
            name: '经典黑色燕尾服套装',
            color: '黑色',
            size: '175/98A',
            price: '8,600',
            image: 'https://images.unsplash.com/photo-1596444382656-f81a489d9190?ixlib=rb-4.0.3&auto=format&fit=crop&w=300&q=80'
          }
        ],
        totalPrice: '8,600',
        createTime: '2024-03-18',
        isToday: false,
        primaryAction: 'detail',
        primaryActionText: '查看详情',
        secondaryAction: 'service',
        secondaryActionText: '售后服务'
      }
    ];
    
    // 将订单分类
    const todayOrders = mockOrders.filter(order => order.isToday);
    const historyOrders = mockOrders.filter(order => !order.isToday);
    
    this.setData({
      allOrders: mockOrders,
      todayOrders: todayOrders,
      historyOrders: historyOrders,
      filteredOrders: mockOrders
    });
  },
  
  // 切换筛选项
  switchFilter: function(e) {
    const filter = e.currentTarget.dataset.filter;
    this.setData({ currentFilter: filter });
    
    // 根据筛选条件更新订单列表
    this.filterOrders();
  },
  
  // 搜索输入
  searchInput: function(e) {
    this.setData({ searchKeyword: e.detail.value });
    
    // 根据搜索关键词更新订单列表
    this.filterOrders();
  },
  
  // 筛选订单列表
  filterOrders: function() {
    const { allOrders, currentFilter, searchKeyword } = this.data;
    
    // 先根据筛选条件筛选
    let filtered = [...allOrders];
    
    if (currentFilter !== 'all') {
      // 根据不同筛选条件筛选
      switch(currentFilter) {
        case 'unpaid':
          filtered = filtered.filter(order => order.status === 'unpaid');
          break;
        case 'pickup':
          filtered = filtered.filter(order => order.status === 'paid');
          break;
        case 'completed':
          filtered = filtered.filter(order => order.status === 'completed');
          break;
        case 'canceled':
          filtered = filtered.filter(order => order.status === 'canceled');
          break;
      }
    }
    
    // 再根据搜索关键词筛选
    if (searchKeyword) {
      filtered = filtered.filter(order => 
        order.orderNumber.includes(searchKeyword) || 
        order.client.name.includes(searchKeyword) ||
        order.items.some(item => item.name.includes(searchKeyword))
      );
    }
    
    // 根据筛选结果更新分类
    const todayOrders = filtered.filter(order => order.isToday);
    const historyOrders = filtered.filter(order => !order.isToday);
    
    this.setData({
      filteredOrders: filtered,
      todayOrders: todayOrders,
      historyOrders: historyOrders
    });
  },
  
  // 查看订单详情
  viewOrderDetail: function(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/order_detail/order_detail?id=${orderId}`
    });
  },
  
  // 处理订单操作
  handleOrderAction: function(e) {
    const orderId = e.currentTarget.dataset.id;
    const action = e.currentTarget.dataset.action;
    
    // 根据不同操作处理
    switch(action) {
      case 'payment':
        this.handlePayment(orderId);
        break;
      case 'appointment':
        this.handleAppointment(orderId);
        break;
      case 'service':
        this.handleAfterSale(orderId);
        break;
      case 'detail':
      default:
        this.viewOrderDetail(e);
        break;
    }
  },
  
  // 处理收款
  handlePayment: function(orderId) {
    wx.showToast({
      title: '暂未实现收款功能',
      icon: 'none'
    });
  },
  
  // 处理安排试装
  handleAppointment: function(orderId) {
    wx.showToast({
      title: '暂未实现预约功能',
      icon: 'none'
    });
  },
  
  // 处理售后服务
  handleAfterSale: function(orderId) {
    wx.showToast({
      title: '暂未实现售后功能',
      icon: 'none'
    });
  },
  
  // 创建新订单
  createNewOrder: function() {
    wx.navigateTo({
      url: '/pages/order_detail/order_detail?mode=add'
    });
  }
}) 